import {
  ElLoading,
  ElMessage,
  ElMessageBox,
  ElNotification,
} from 'element-plus'

/**
 * @type {import('element-plus').LoadingInstance | null}
 */
let loadingInstance = null

const defaultTitle = '系统提示'

/**
 * 显示消息提示
 * @param {'success'|'warning'|'info'|'error'} type
 * @param {string} content
 */
function showMessage(type, content) {
  ElMessage({
    type,
    message: content,
    plain: true,
  })
}

/**
 * 显示弹窗提示
 * @param {'success'|'warning'|'info'|'error'} type
 * @param {string} content
 */
function showAlert(type, content) {
  ElMessageBox.alert(content, defaultTitle, { type })
}

/**
 * 显示通知
 * @param {'success'|'warning'|'info'|'error'} type
 * @param {string} content
 */
function showNotification(type, content) {
  ElNotification[type](content)
}

/**
 * Modal 工具集
 * @typedef {Object} Modal
 * @property {(content: string) => void} msg - 普通信息提示
 * @property {(content: string) => void} msgError - 错误信息提示
 * @property {(content: string) => void} msgSuccess - 成功信息提示
 * @property {(content: string) => void} msgWarning - 警告信息提示
 * @property {(content: string) => void} alert - 普通弹窗提示
 * @property {(content: string) => void} alertError - 错误弹窗提示
 * @property {(content: string) => void} alertSuccess - 成功弹窗提示
 * @property {(content: string) => void} alertWarning - 警告弹窗提示
 * @property {(content: string) => void} notify - 普通通知
 * @property {(content: string) => void} notifyError - 错误通知
 * @property {(content: string) => void} notifySuccess - 成功通知
 * @property {(content: string) => void} notifyWarning - 警告通知
 * @property {(content: string, options?: import('element-plus').MessageBoxOptions) => Promise<any>} confirm - 确认框
 * @property {(content: string, options?: import('element-plus').MessageBoxOptions) => Promise<{ value: string }>} prompt - 提示输入框
 * @property {(content?: string) => void} loading - 打开遮罩层
 * @property {() => void} closeLoading - 关闭遮罩层
 */

/** @type {Modal} */
const modal = {
  msg: content => showMessage('info', content),
  msgError: content => showMessage('error', content),
  msgSuccess: content => showMessage('success', content),
  msgWarning: content => showMessage('warning', content),

  alert: content => ElMessageBox.alert(content, defaultTitle),
  alertError: content => showAlert('error', content),
  alertSuccess: content => showAlert('success', content),
  alertWarning: content => showAlert('warning', content),

  notify: content => showNotification('info', content),
  notifyError: content => showNotification('error', content),
  notifySuccess: content => showNotification('success', content),
  notifyWarning: content => showNotification('warning', content),

  confirm(content, options = {}) {
    return ElMessageBox.confirm(content, defaultTitle, {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      ...options,
    })
  },

  prompt(content, options = {}) {
    return ElMessageBox.prompt(content, defaultTitle, {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      ...options,
    })
  },

  loading(content = '加载中...') {
    loadingInstance = ElLoading.service({
      lock: true,
      text: content,
      background: 'rgba(0, 0, 0, 0.7)',
    })
  },

  closeLoading() {
    if (loadingInstance) {
      loadingInstance.close()
      loadingInstance = null
    }
  },
}

export default modal
